package tv.scolopender.json;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import tv.scolopender.model.Comment;
import tv.scolopender.model.Scolo;
import tv.scolopender.model.User;

public class CommentJSON {
	static String location = "http://hdmk-portal.net/danny/scolopenderTV/";
	static String coding = "UTF-8";

	public static ArrayList<Comment> selectComments(int sid, int offset,
			int amount) {
		ArrayList<Comment> comments = new ArrayList<Comment>();
		String result = "";
		try {
			ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
			nameValuePairs.add(new BasicNameValuePair("offset", offset + ""));
			nameValuePairs.add(new BasicNameValuePair("amount", amount + ""));
			nameValuePairs.add(new BasicNameValuePair("sid", sid + ""));

			HttpClient httpclient = new DefaultHttpClient();
			HttpPost httppost = new HttpPost(location + "selectComments.php");
			httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
			HttpResponse response = httpclient.execute(httppost);
			HttpEntity entity = response.getEntity();
			InputStream is = entity.getContent();
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					is, coding), 8);
			StringBuilder sb = new StringBuilder();
			String line = null;

			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}

			is.close();

			result = sb.toString();
			JSONArray jArray = new JSONArray(result);

			for (int i = 0; i < jArray.length(); i++) {
				Comment c = new Comment();
				JSONObject json_data = jArray.getJSONObject(i);

				c.setId(json_data.getInt("cid"));
				c.setText(json_data.getString("text"));
				c.setUser(new User(json_data.getString("uid")));

				comments.add(c);
			}
		} catch (Exception e) {
			return new ArrayList<Comment>();
		}
		return comments;
	}

	public static void insertComment(Scolo s, Comment c) {
		try {
			ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
			nameValuePairs.add(new BasicNameValuePair("uid", c.getUser()
					.getId()));
			nameValuePairs.add(new BasicNameValuePair("sid", s.getId() + ""));
			nameValuePairs.add(new BasicNameValuePair("text", c.getText()));

			// http post
			HttpClient httpclient = new DefaultHttpClient();
			HttpPost httppost = new HttpPost(location + "insertComment.php");
			httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
			HttpResponse response = httpclient.execute(httppost);
			HttpEntity entity = response.getEntity();
			InputStream is = entity.getContent();
			// convert response to string
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					is, coding), 8);
			StringBuilder sb = new StringBuilder();
			String line = null;

			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}

			is.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
